import { defineConfig } from 'vite'
import { fileURLToPath, URL } from 'node:url'
import uni from '@dcloudio/vite-plugin-uni'

// 路由映射表
const routeMap = {
  '/home': '/pages/index/index',
  '/tasks': '/pages/tasks/index',
  '/profile': '/pages/users/profile',
  '/login': '/pages/login/login',
  '/wallet': '/pages/wallet/account',
  '/team': '/pages/team/index',
  '/fenxiao': '/pages/fenxiao/index',
  '/search': '/pages/index/search',
  '/orders': '/pages/order/orderlist',
  '/shop': '/pages/shop/orderlist',
  '/notice': '/pages/notice/message',
  '/realname': '/pages/realname/index',
  '/router-test': '/pages/test/router-test'
}

export default defineConfig({
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  define: {
    // 禁用 SharedArrayBuffer 相关功能
    __SHARED_ARRAY_BUFFER__: 'undefined',
    __ATOMICS__: 'undefined'
  },
  plugins: [
    uni(),
    // 自定义插件处理路由重写
    {
      name: 'custom-route-rewrite',
      configureServer(server) {
        server.middlewares.use((req, res, next) => {
          const url = req.url?.split('?')[0] // 移除查询参数
          
          // 检查是否是自定义路由
          if (routeMap[url]) {
            req.url = req.url.replace(url, routeMap[url])
          }
          
          next()
        })
      }
    }
  ],
  server: {
    headers: {
      // 设置安全头部，禁用 SharedArrayBuffer
      'Cross-Origin-Embedder-Policy': 'unsafe-none',
      'Cross-Origin-Opener-Policy': 'unsafe-none'
    },
    proxy: {
      '/api': {
        target: 'https://static.shixiaoquan.com/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/api')
      }
    }
  }
})


